package com.dunqi.gpm.cangxi.develop.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.TextView;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.base.ComObserver;
import com.dunqi.gpm.cangxi.common.base.Data;
import com.dunqi.gpm.cangxi.common.util.CommonUtil;
import com.dunqi.gpm.cangxi.common.util.JsonUtils;
import com.dunqi.gpm.cangxi.common.util.TimeUtil;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.common.view.NoScrollGridView;
import com.dunqi.gpm.cangxi.home.adapter.PhotoAdapter;
import com.dunqi.gpm.cangxi.home.bean.ProjectWorkTrend;
import com.dunqi.gpm.cangxi.home.bean.RemarkPic;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.widget.NormalView;
import com.dunqi.gpm.cangxi.mine.util.UserUtil;
import com.foamtrace.photopicker.intent.PhotoPreviewIntent;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

import static com.dunqi.gpm.cangxi.home.constance.Constance.REQUEST_PREVIEW_CODE;

public class ProjectTrendDetailActivity extends BaseAct implements AdapterView.OnItemClickListener {

    private static final String PROWORKTRENDID = "proWorkTrendId";
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.title_view)
    NormalView titleView;
    @BindView(R.id.kind_view)
    NormalView kindView;
    @BindView(R.id.project_name_view)
    NormalView projectNameView;
    @BindView(R.id.issue_org_name_view)
    NormalView issue_org_name_view;
    @BindView(R.id.time_view)
    NormalView timeView;
    @BindView(R.id.show_order_view)
    NormalView showOrderView;
    @BindView(R.id.leave_message_tv)
    TextView leaveMessageTv;

    @BindView(R.id.trend_audit)
    TextView trendAuditBtn;

    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.no_grid_view)
    NoScrollGridView noGridView;

    ArrayList<String> data = new ArrayList<>();
    private PhotoAdapter adapter;
    private long proWorkTrendId;
    private ProjectWorkTrend item;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .getProjectWorkTrendInfo(proWorkTrendId)
                .compose(RxSchedulers.<Data<ProjectWorkTrend>>io_main())
                .subscribe(new ComObserver<ProjectWorkTrend>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }

                    @Override
                    public void onSuccess(ProjectWorkTrend projectWorkTrend) {
                        if (projectWorkTrend == null) {
                            return;
                        }
                        item = projectWorkTrend;
                        refreshUi();

                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    private void refreshUi() {

        issue_org_name_view.setText(item.getIssueOrgName());
        titleView.setText(item.getTrendTitile());
        kindView.setText(item.getTrendTypeText());
        timeView.setText(TimeUtil.getTen(item.getIssueTime()));
        if (Constance.WORK_TREND_TYPE_PROJEC_TREND.equalsIgnoreCase(item.getTrendType())) {
            projectNameView.setText(item.getProjectName());
            projectNameView.setVisibility(View.VISIBLE);
        }


        String auditSubmitAttachs = item.getPicRemarkRecords();
        if (!TextUtils.isEmpty(auditSubmitAttachs)) {
            ArrayList<RemarkPic> pics = JsonUtils.fromJson(auditSubmitAttachs, new TypeToken<ArrayList<RemarkPic>>() {
            }.getType());
            data.clear();
            for (RemarkPic pic : pics) {
                data.add(UserUtil.getBaseUrl() + pic.getUrl());
            }

            adapter = new PhotoAdapter(getActivity(), data);
            noGridView.setAdapter(adapter);
            noGridView.setOnItemClickListener(this);
        }

        String htmlContent = item.getTrendHtmlContent();
        if (!TextUtils.isEmpty(htmlContent)) {
            CommonUtil.initWebViewParameter(webView);
            //bug66 图2 如果需要完全展示，注释上一行，打开下一行注释
//            WebUtil.webSetting(webView,this,false);
            webView.loadDataWithBaseURL(null, htmlContent, "text/html", "utf-8", null);
        }
    }

    @Override
    protected void initUiAndListener() {
        proWorkTrendId = getIntent().getLongExtra(PROWORKTRENDID, 0);
        final int levelType = UserUtil.getLevelType();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
            setResult(200);
            finish();
        }
    }

    public static void start(Activity context, long proWorkTrendId) {
        Intent intent = new Intent(context, ProjectTrendDetailActivity.class);
        intent.putExtra(PROWORKTRENDID, proWorkTrendId);
        //context.startActivity(intent);
        context.startActivityForResult(intent, 611);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_trend_detail;
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        PhotoPreviewIntent intent = new PhotoPreviewIntent(this);
        intent.setCurrentItem(position); // 当前选中照片的下标
        intent.setPhotoPaths(data); // 已选中的照片地址
        startActivityForResult(intent, REQUEST_PREVIEW_CODE);
    }
}
